package top.tenebrous.yzl.context;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.tenebrous.yzl.enums.TaskStatus;

import java.util.UUID;

/**
 * @Description:
 * @Project: jestic-spring-boot
 * @Package: com.zy.tenebrous.jestic.chain
 * @Author: zhangya
 * @CreateTime: 2023-06-28  17:57
 * @Version: 1.0.0
 */
@Data
@Slf4j
public class JesticTask {

    /**
     * 任务唯一标识符
     */
    private String seqId = UUID.randomUUID().toString();

    /**
     * 任务启动时间
     */
    private Long applyTime;

    /**
     * 任务状态
     */
    private TaskStatus status;

    /**
     * 任务节点
     */
    private String node;

    /**
     * 上一个任务节点
     */
    private String lastNode;

    /**
     * 启动任务
     */
    public void start() {
        if (TaskStatus.NEW != this.status) {
            log.info("任务[{}]已经启动...", this.seqId);
            return;
        }
        this.applyTime = System.currentTimeMillis();
        this.status = TaskStatus.RUNNING;
        log.info("任务[{}]启动成功...", this.seqId);
    }

    public void updateNode(String taskEnum) {
        // 如果当前节点为空，则任务启动，设置当前节点
        if (node == null) {
            node = taskEnum;
            lastNode = taskEnum;
            return;
        }
        lastNode = node;
        node = taskEnum;
    }

}
